﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        bool drawing_polygon = false;
        Polygon ghost;
        public MainWindow()
        {
            InitializeComponent();
        }

        private void canvasMouseLeftButtonHanler(object sender, MouseButtonEventArgs e)
        {
            if (!drawing_polygon)
            {
                return;
            }
            ghost.Points.Add(Mouse.GetPosition(canvas1));

        }

        private void canvasMouseMoveHandler(object sender, RoutedEventArgs e)
        {
            if (!drawing_polygon)
            {
                return;
            }
            if (ghost.Points.Count > 0)
            {
                ghost.Points.RemoveAt(ghost.Points.Count - 1);
                ghost.Points.Add(Mouse.GetPosition(canvas1));
            }   
        }

        private void showPolygon_Click(object sender, RoutedEventArgs e)
        {
            ghost.Points.Clear();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            button3_Click(sender, e);
            string sUri = @"C:\Users\Yoni\Pictures\";
            sUri += textBox1.Text;
            Uri src = new Uri(sUri, UriKind.RelativeOrAbsolute);
            Console.WriteLine(src.AbsolutePath.ToString());
            BitmapImage bmp = new BitmapImage(src);
            image1.Source = bmp;
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            drawing_polygon = true;
            ghost = new Polygon();
            ghost.Stroke = Brushes.Aqua;
            canvas1.Children.Add(ghost); 
        }

        private void canvas1_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            ghost.Points.RemoveAt(ghost.Points.Count - 1);
            ghost.Stroke = Brushes.Yellow;
            drawing_polygon = false;
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {

            List<Polygon> to_remove = new List<Polygon>();
            foreach (UIElement el in canvas1.Children)
            {
                if (el.GetType() == typeof(Polygon))
                {
                    to_remove.Add((Polygon)el);
                }
            }
            foreach (Polygon pol in to_remove)
            {
                canvas1.Children.Remove(pol);
            }
        }

        private void writeDataToXML(string name)
        {
            if (name.Length <= 0)
            {
                MessageBox.Show("Enter a file name");
                return;
            }
            if (image1.Source == null)
            {
                MessageBox.Show("Choose a background to work on");
                return;
            }
            Console.WriteLine("Writing to " + name + ".xml...");
            
            XmlTextWriter textWriter = new XmlTextWriter("C:\\workspace\\videoGames\\delete\\" + name + ".xml", null);
            // Opens the document
            textWriter.WriteStartDocument();
            textWriter.WriteStartElement(name);
            textWriter.WriteStartElement("BackgroundPath");
            textWriter.WriteString(image1.Source.ToString());
            textWriter.WriteEndElement();
            textWriter.WriteStartElement("BackgroundWidth");
            textWriter.WriteString(image1.Width.ToString());
            textWriter.WriteEndElement();
            textWriter.WriteStartElement("BackgroundHeight");
            textWriter.WriteString(image1.Height.ToString());
            textWriter.WriteEndElement();

            //write polygons
            foreach (UIElement el in canvas1.Children)
            {
                if (el.GetType() == typeof(Polygon))
                {
                    writePolygon(textWriter, (Polygon)el);
                }
            }

            // Ends the document.
            textWriter.WriteEndDocument();
            // close writer
            textWriter.Close();
        }

        private void writePolygon(XmlTextWriter textWriter, Polygon polygon)
        {
            textWriter.WriteStartElement("Polygon");
            textWriter.WriteStartElement("NumPoints");
            textWriter.WriteString(polygon.Points.Count.ToString());
            textWriter.WriteEndElement();
            foreach (Point p in polygon.Points)
            {
                textWriter.WriteStartElement("Point");
                textWriter.WriteStartElement("X");
                textWriter.WriteString(p.X.ToString());
                textWriter.WriteEndElement();
                textWriter.WriteStartElement("Y");
                textWriter.WriteString(p.Y.ToString());
                textWriter.WriteEndElement();
                textWriter.WriteEndElement();
            }
            textWriter.WriteEndElement();
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            writeDataToXML(filenameTextbox.Text);
        }
        
    }
}
